/*
 * CAlarmObject.cpp
 *
 *  Created on: Apr 15, 2009
 *      Author: Cedric
 */

//includes
#include "AlarmObject.h"
#include "MyS60Alarm.rsg"
#include "AlarmRepeatDef.h"

// ============================ MEMBER FUNCTIONS ===============================

// -----------------------------------------------------------------------------
// CAlarmObject::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CAlarmObject* CAlarmObject::NewL(TASShdAlarm* aAlarm)
	{
	CAlarmObject* self = CAlarmObject::NewLC(aAlarm);
	CleanupStack::Pop(self);
	return self;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CAlarmObject* CAlarmObject::NewLC(TASShdAlarm* aAlarm)
	{
	CAlarmObject* self = new (ELeave) CAlarmObject;
	CleanupStack::PushL(self);
	self->ConstructL(aAlarm);
	return self;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::~CAlarmObject()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CAlarmObject::~CAlarmObject()
	{
	if (iAlarm)
		{
		delete iAlarm;
		}
	}

// -----------------------------------------------------------------------------
// CAlarmObject::SetNextDueTime( TTime aTime )
// Set alarm's next due time
// @aTime, must be UTC time
// -----------------------------------------------------------------------------
//	
void CAlarmObject::SetNextDueTime( TTime aTime )
	{
	iAlarm->SetUtcNextDueTime( aTime );
	return;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::SetRepeatDefinition( TAlarmRepeatDefinition aDef )
// @aDef, must be one of the five repeat definitions 
// -----------------------------------------------------------------------------
//
void CAlarmObject::SetRepeatDefinition( TAlarmRepeatDefinition aDef )
	{
	TAlarmRepeatDefinition& repDef = iAlarm->RepeatDefinition();
	repDef = aDef;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::SetMessage( TAlarmMessage aMessage )
// set alert message for the alarm
// -----------------------------------------------------------------------------
//
void CAlarmObject::SetMessage( TDesC* aMessage )
	{
	TAlarmMessage& message = iAlarm->Message();
	message = *aMessage;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::AlarmId()
// return alarm id in text format
// -----------------------------------------------------------------------------
//
TDesC* CAlarmObject::AlarmId()
	{
	TAlarmId idInt = iAlarm->Id();
	TBuf<10> *idText;
	idText->Format(_L("%d"),idInt);
	return idText;
	}

// -----------------------------------------------------------------------------
// CAlarmObject:: NextDueTimeL()
// return next due time of the alarm
// -----------------------------------------------------------------------------
//
TDesC* CAlarmObject:: NextDueTimeL()
	{
	TTime time = iAlarm->NextDueTime();
	TBuf<16> timeText = TBuf<16>();

	_LIT(KTimeFormat, "%-B%:0%J%:1%T%:2%S%:3%+B");
	TRAPD(result, time.FormatL(timeText, KTimeFormat))

	return &timeText;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::RepeatDefinition()
// return next repeat definition of the alarm
// -----------------------------------------------------------------------------
//
TDesC* CAlarmObject::RepeatDefinition()
	{
	TDesC* repDefText;
	*repDefText = KRepDefTextNotSet;
	
	TAlarmRepeatDefinition repDef = iAlarm->RepeatDefinition();
	switch (repDef)
		{
		case EAlarmRepeatDefintionRepeatOnce:
			*repDefText = KRepDefTextOnce;
			break;
		case EAlarmRepeatDefintionRepeatNext24Hours:
			*repDefText = KRepDefTextN24H;
			break;
		case EAlarmRepeatDefintionRepeatDaily:
			*repDefText = KRepDefTextDaily;
			break;
		case EAlarmRepeatDefintionRepeatWorkday:
			*repDefText = KRepDefTextWorkday;
			break;
		case EAlarmRepeatDefintionRepeatWeekly:
			*repDefText = KRepDefTextWeekly;
			break;
		default:
			break;
		}
	
	return repDefText;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::Message()
// retun alarm message
// -----------------------------------------------------------------------------
//
TDesC* CAlarmObject::Message()
	{
	TAlarmMessage message = iAlarm->Message();
	return &message;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::Alarm()
// retun alarm
// -----------------------------------------------------------------------------
//
TASShdAlarm* CAlarmObject::Alarm()
	{
	return iAlarm;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CAlarmObject::ConstructL(TASShdAlarm* aAlarm)
	{
	iAlarm = aAlarm;
	}

// -----------------------------------------------------------------------------
// CAlarmObject::CAlarmObject()
// default c++ constructor
// -----------------------------------------------------------------------------
//
CAlarmObject::CAlarmObject()
	{
	//no implementation needed.
	}

//end of file
